var adComapyName = "";

var page = 1; //默认页数
var pageSize = 10; //每页条数
var total = 0; //总条数
//分页方法
function paging(page, pageSize, total) {
	page = Number(page);

	//最大页数
	var maxPage = Math.ceil(total / pageSize) || 1;
	if(page > maxPage) {
		//取值不能超过maxPage
		page = Math.min(page, maxPage);
		tiaozhuan(param, page, pageSize, adComapyName);
	} else if(page < 1) {
		//取值不能小于1
		page = Math.max(page, 1);
		tiaozhuan(param, page, pageSize, adComapyName);
	} else {
		//	当前页数
		$('.ui-paging-count').val(page);
		//首页的置灰处理
		$('.js-page-first').removeClass('ui-pager-disabled');
		//上一页
		$('.js-page-prev').removeClass('ui-pager-disabled').attr("data-page", page - 1);
		//下一页
		$('.js-page-next').removeClass('ui-pager-disabled').attr("data-page", page + 1);
		//最末页
		$('.js-page-last').removeClass('ui-pager-disabled').attr("data-page", maxPage);
		//总页数显示
		$('.js-page-total').text(maxPage);

		//当前显示多少页的处理
		$(".ui-select-pagesize").val(pageSize);
		//第一页的处理
		if(page == 1) {
			$(".js-page-first").addClass('ui-pager-disabled').attr("data-page", "");
			$(".js-page-prev").addClass('ui-pager-disabled').attr("data-page", "");
		}
		if(page == maxPage) {
			//最后一页的处理
			$(".js-page-last").addClass('ui-pager-disabled').attr("data-page", "");
			$(".js-page-next").addClass('ui-pager-disabled').attr("data-page", "");
		}
	}

};
//下拉选择分页条数的操作
$('.ui-select-pagesize').on("change", function() {
	pageSize = $(this).val();
	tiaozhuan(param, page, pageSize, adComapyName);
});

//跳转到X页  blur
$(".ui-paging-count").on("blur", function() {
	page = $(this).val();
	tiaozhuan(param, page, pageSize, adComapyName);
});

//上一页，下一页，首页，最末页的跳转
$('.ui-pager').on("click", function() {
	page = $(this).attr("data-page");
	tiaozhuan(param, page, pageSize, adComapyName);
});

//角色名查找
$('.clickFunc').on("click", function() {
	adComapyName = $('#searchRalename').val();
	tiaozhuan(param, page, pageSize, adComapyName);
});

function tiaozhuan(param, page, pageSize, adComapyName) {
	var html = "";
	method = "post";
	datas = {
		page: page,
		pageSize: pageSize,
		adComapyName: adComapyName
	};
	getAgentMsg(param, method, datas);

	for(var i = 0; i < dataResult.data.rows.length; i++) {
		html += "<tr>";
		html += "<td>" + dataResult.data.rows[i].id + "</td>";
		html += "<td>" + dataResult.data.rows[i].adverName + "</td>";
		html += "<td>" + dataResult.data.rows[i].balance + "</td>";
		html += "<td><a href='profitDetail.html?token=" + token + "&id=" + token_id + "&adverId=" + dataResult.data.rows[i].id + "&type=1'>查看记录</a>&nbsp;&nbsp;&nbsp;&nbsp;";
		html += "<a href='javascript:void(0);' onclick='showMaskin(this)'>转入资金</a>&nbsp;&nbsp;&nbsp;&nbsp;";
		html += "<a href='javascript:void(0);' onclick='showMaskto(this)'>转出资金</a></td>";
		html += "</tr>";
	}
	$(".roleList").html(html);
	//之后处理分页
	total = dataResult.data.total;
	paging(page, pageSize, total);
}
//下载文件
function download(){
	var param2 = "/agency/fiInformation/selectByAdname";
	var method2 = "post";
	var adComapyName = $('#searchRalename').val();
	var datas2 = {page: page, pageSize: 1000,adComapyName:adComapyName};
	getAgentMsg2(param2,method2,datas2);
	exportCsv({
        title: ['广告主ID','广告主名称','现金余额'],
        titleForKey: ['id','adverName','balance'],
        data: dataResult2.data.rows,
    },"财务信息.csv");
}
//转入资金打开模态框
function showMaskin(obj) {
	$("#layui-layer7").find(".accountName").text($(obj).parent().prev().prev().text());
	$("#layui-layer7").find(".number").text($(".usableBalance").text());
	$("#layui-layer7").find(".adUserId").val($(obj).parent().prev().prev().prev().text());
	$("#layui-layer7").show();
	$("#popLayer").show();
}

//转入资金保存按钮
function transferin(){
	 var param2 = "/agency/fiInformation/recharge";
	 var method2 = "post";
	 var adUserId = $("#layui-layer7").find(".adUserId").val();
	 var balance = Number($("#layui-layer7").find(".money").val());
	 var number2 = Number($("#layui-layer7").find(".number").text());
	 if(balance>number2){
	 	layer.msg("可分配余额不足，请重新输入");
	 	return false;
	 }
	 var typein = 2;
	 var datas2 = {adUserId:adUserId,balance:balance,type:typein};
	 getAgentMsg2(param2, method2, datas2);
	 $("#layui-layer7").hide();
	$("#popLayer").hide();

	 layer.msg(dataResult2.msg,{time:2*1000},function() {
		window.location.reload();
	});	
}


//转出资金关闭模态框
$(".layui-layer-close1").on("click", function() {
	$("#layui-layer7").hide();
	$("#popLayer").hide();
});

//转出资金打开模态框
function showMaskto(obj) {
	$("#layui-layer4").find(".accountName").text($(obj).parent().prev().prev().text());
	$("#layui-layer4").find(".number").text($(obj).parent().prev().text());
	$("#layui-layer4").find(".adUserId").val($(obj).parent().prev().prev().prev().text());
	$("#layui-layer4").show();
	$("#popLayer").show();
}

//转出资金保存按钮
function transferto(){
	 var param2 = "/agency/fiInformation/recharge";
	 var method2 = "post";
	 var adUserId = $("#layui-layer4").find(".adUserId").val();
	 var balance = Number($("#layui-layer4").find(".money").val());
	 var number2 = Number($("#layui-layer4").find(".number").text());
	 if(balance>number2){
	 	layer.msg("可转出余额不足，请重新输入");
	 	return false;
	 }
	 var typein = 4;
	 var datas2 = {adUserId:adUserId,balance:balance,type:typein};
	 getAgentMsg2(param2, method2, datas2);
	 $("#layui-layer4").hide();
	$("#popLayer").hide();

	 layer.msg(dataResult2.msg,{time:2*1000},function() {
		window.location.reload();
	});	
}

//转出资金关闭模态框
$(".layui-layer-close2").on("click", function() {
	$("#layui-layer4").hide();
	$("#popLayer").hide();
});

//获取余额通用
function getAllMoney() {
	$.ajax({
		url: url + "/agency/FiTradeList/showbalancebyuserid",
		type: "post",
		dataType: 'json',
		data: token_id,
		contentType: "application/json",
		useDefaultXhrHeader: false,
		 beforeSend: function(XMLHttpRequest) {
			XMLHttpRequest.setRequestHeader("X-Token", token); 
		},
		success: function(result) {

			if(result.code == '0') {
//				console.log(result);
				$(".allBalance").text(result.data.accountBalance); //账户余额
				$(".usableBalance").text(result.data.convertibleBalance); //可转余额

			} else {
				isErr(result);
			}
		},
		err: function(err) {
			layer.msg("请求失败");
		}
	});
}

//初始化加载数据
window.onload = function() {

	getAllMoney();
	param = "/agency/fiInformation/selectByAdname";
	tiaozhuan(param, page, pageSize, adComapyName);

}